package net.caixiaomi.info.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BaseAlertDialog extends AlertDialog {
    @BindView(android.R.id.message)
    TextView mMessage;

    @BindView(R.id.title)
    TextView mTitleTv;

    @BindView(android.R.id.button1)
    TextView mBtnPositive;
    @BindView(android.R.id.button2)
    TextView mBtnNegative;
    @BindView(R.id.divider)
    View mDivider;


    @OnClick(android.R.id.button1)
    void toPositive(View view) {
        if (mPositiveButtonListener != null) {
            mPositiveButtonListener.onClick(view);
        }
        dismiss();
    }

    @OnClick(android.R.id.button2)
    void toNegative(View view) {
        if (mNegativeButtonListener != null) {
            mNegativeButtonListener.onClick(view);
        }
        dismiss();
    }

    private CharSequence mText;
    private CharSequence mPositiveButtonText;
    private CharSequence mNegativeButtonText;
    private CharSequence mTitle;
    private View.OnClickListener mPositiveButtonListener;
    private View.OnClickListener mNegativeButtonListener;

    public BaseAlertDialog(@NonNull Context context) {
        super(context, R.style.AlertDialogStyle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_base_message);
        ButterKnife.bind(this);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
    }

    public void setTitle(String title){
        mTitle=title;
    }

    @Override
    public void setMessage(CharSequence message) {
        mText = message;
    }

    public void setMessage(@StringRes int textId){
        mText = CommonApp.getInstance().getString(textId);
    }

    public void setPositiveButton(@StringRes int textId, View.OnClickListener listener) {
        setPositiveButton(CommonApp.getInstance().getString(textId), listener);
    }

    public void setPositiveButton(CharSequence text, View.OnClickListener listener) {
        mPositiveButtonText = text;
        mPositiveButtonListener = listener;
    }

    public void setNegativeButton(@StringRes int textId, View.OnClickListener listener) {
        setNegativeButton(CommonApp.getInstance().getString(textId), listener);
    }

    public void setNegativeButton(CharSequence text, View.OnClickListener listener) {
        mNegativeButtonText = text;
        mNegativeButtonListener = listener;
    }

    @Override
    public void show() {
        super.show();
        if (!TextUtils.isEmpty(mText)) {
            mMessage.setText(mText);
        }
        if(!TextUtils.isEmpty(mTitle)){
            mTitleTv.setText(mTitle);
            mTitleTv.setVisibility(View.VISIBLE);
        }

        if (!TextUtils.isEmpty(mPositiveButtonText)) {
            mBtnPositive.setText(mPositiveButtonText);
        }
        if (!TextUtils.isEmpty(mNegativeButtonText)) {
            mBtnNegative.setText(mNegativeButtonText);
            mDivider.setVisibility(View.VISIBLE);
            mBtnNegative.setVisibility(View.VISIBLE);
        }else{
            mDivider.setVisibility(View.GONE);
            mBtnNegative.setVisibility(View.GONE);
        }
//        mMessage.setText(mText);
//        getWindow().setContentView(mContentView);
    }
}
